
package com.lzb.compressmaster.dialog;

import android.content.Context;
import android.media.MediaPlayer;
import android.os.Handler;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.lzb.compressmaster.R;
import com.lzb.compressmaster.basic.BaseSmartDialog;
import com.lzb.compressmaster.databinding.DialogAudioPreviewBinding;
import com.lzb.compressmaster.utils.MediaUtil;
import com.lzb.compressmaster.utils.TimeUtil;

import java.io.IOException;

public class AudioPreviewDialog extends BaseSmartDialog<DialogAudioPreviewBinding> implements View.OnClickListener {

    private MediaPlayer mediaPlayer = new MediaPlayer();
    private String audioPath;
    private long audioLength;
    private Handler mHandler;
    private final Runnable mTaskUpdateTime = new Runnable() {
        @Override
        public void run() {
            mDataBinding.tvPlayLeft.setText(TimeUtil.getMmss(mediaPlayer.getCurrentPosition()));
            mDataBinding.sbPlay.setProgress(mediaPlayer.getCurrentPosition() / 1000);
            mHandler.postDelayed(this, 500);
        }
    };

    public AudioPreviewDialog(@NonNull Context context) {
        super(context);
    }

    public void setAudioPath(String audioPath) {
        this.audioPath = audioPath;
    }

    @Override
    protected boolean canceledOnTouchOutside() {
        return true;
    }

    @Override
    protected int getGravity() {
        return Gravity.CENTER;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.dialog_audio_preview;
    }

    @Override
    protected void initView(View view) {
        mDataBinding.ivCancel.setOnClickListener(this);
        mDataBinding.ivPlay.setOnClickListener(this);

        mDataBinding.tvName.setText(FileUtils.getFileName(audioPath));
        mDataBinding.tvExtension.setText(getContext().getString(R.string.format_tip) + FileUtils.getFileExtension(audioPath));

        try {
            setMedia();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.ivCancel) {
            dismiss();
        } else if (id == R.id.ivPlay) {
            if (mediaPlayer.isPlaying()) {
                mediaPlayer.pause();
                mDataBinding.ivPlay.setImageResource(R.drawable.abf);
            } else {
                mediaPlayer.start();
                mDataBinding.ivPlay.setImageResource(R.drawable.azt);
            }
        }
    }

    private void setMedia() throws IOException {
        mHandler = new Handler();
        audioLength = MediaUtil.getDuration(audioPath);
        //设置seekbar
        mDataBinding.sbPlay.setMax((int) (audioLength / 1000));
        mDataBinding.tvPlayLeft.setText("00:00");
        mDataBinding.tvPlayRight.setText(TimeUtil.getMmss(audioLength));
        mediaPlayer.setDataSource(audioPath);
        mediaPlayer.prepare();
        mediaPlayer.seekTo(1);
        mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {  // 播放完成的监听
                mDataBinding.tvPlayLeft.setText("00:00");
                mDataBinding.sbPlay.setProgress(0);
                mp.seekTo(1);
                stopTime();
            }
        });
        //直接播放音频
        mediaPlayer.start();
        mDataBinding.ivPlay.setImageResource(R.drawable.azt);
        startTime();
    }

    private void startTime() {
        mHandler.post(mTaskUpdateTime);
    }

    private void stopTime() {
        mHandler.removeCallbacks(mTaskUpdateTime);
    }

    @Override
    protected void onStop() {
        super.onStop();
        mediaPlayer.pause();
        stopTime();
        mDataBinding.ivPlay.setImageResource(R.drawable.abf);
    }

    private Ilistener listener;

    public void setListener(Ilistener listener) {
        this.listener = listener;
    }

    public interface Ilistener {
        void clickAddFolder(String name);
    }
}
